package com.chushouya.controller.manager.clerk;

import com.chushouya.common.annotations.AuthValidate;
import com.chushouya.common.constant.ServiceType;
import com.general.framework.core.enums.BusinessType;
import com.general.framework.core.lang.Beans;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.Log;
import com.chushouya.manager.dao.entity.UserPaymentMethodEntity;
import com.chushouya.manager.dto.admin.payment.UserPaymentMethodApiDTO;
import com.chushouya.manager.dto.admin.payment.UserPaymentMethodSaveDTO;
import com.chushouya.manager.service.common.UserPaymentMethodApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户收款方式接口
 */
@RestController
@RequestMapping(ServiceType.CLERK + "/userPaymentMethod")
@Api(tags = ServiceType.CLERK + "用户收款方式接口")
@Slf4j
public class UserPaymentMethodClerkController {

    @Resource
    private UserPaymentMethodApiService userPaymentMethodApiService;

    @ApiOperation("根据用户ID查询收款方式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Long", paramType = "query", required = true)
    })
    @GetMapping("/getByUserId")
    @AuthValidate
    public Response<UserPaymentMethodApiDTO> getByUserId(Long userId) {
        return Response.success(userPaymentMethodApiService.selectByUserId(userId));
    }

    @ApiOperation("根据收款方式ID查询详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paymentMethodId", value = "收款方式ID", dataType = "Long", paramType = "query", required = true)
    })
    @GetMapping("/getById")
    @AuthValidate
    public Response<UserPaymentMethodApiDTO> getById(Long paymentMethodId) {
        return Response.success(userPaymentMethodApiService.selectById(paymentMethodId));
    }

    @ApiOperation("创建收款方式")
    @PostMapping("/add")
    @Log(businessType = BusinessType.INSERT)
    @AuthValidate
    public Response<Long> create(@Validated @RequestBody UserPaymentMethodSaveDTO saveDTO) {
        Long paymentMethodId = userPaymentMethodApiService.create(saveDTO);
        return Response.success(paymentMethodId);
    }

    @ApiOperation("编辑收款方式")
    @PostMapping("/edit")
    @Log(businessType = BusinessType.UPDATE)
    @AuthValidate
    public Response<Void> edit(@Validated @RequestBody UserPaymentMethodSaveDTO saveDTO) {
        userPaymentMethodApiService.edit(saveDTO);
        return Response.success();
    }

    /**
     * 转换为API DTO
     */
    private UserPaymentMethodApiDTO convertToApiDTO(UserPaymentMethodEntity entity) {
        UserPaymentMethodApiDTO dto = Beans.copy(entity, UserPaymentMethodApiDTO.class);
        
        // 设置收款方式类型描述
        if (entity.getPaymentType() != null) {
            dto.setPaymentTypeText(entity.getPaymentType() == 1 ? "支付宝" : "微信");
        }
        
        return dto;
    }
}
